/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistencia;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author hellwolf
 */
@Entity
@Table(name = "factura")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Factura.findAll", query = "SELECT f FROM Factura f"),
    @NamedQuery(name = "Factura.findById", query = "SELECT f FROM Factura f WHERE f.id = :id"),
    @NamedQuery(name = "Factura.findByIva", query = "SELECT f FROM Factura f WHERE f.iva = :iva"),
    @NamedQuery(name = "Factura.findByIrpf", query = "SELECT f FROM Factura f WHERE f.irpf = :irpf"),
    @NamedQuery(name = "Factura.findByRazonSocial", query = "SELECT f FROM Factura f WHERE f.razonSocial = :razonSocial"),
    @NamedQuery(name = "Factura.findByTipoDocumento", query = "SELECT f FROM Factura f WHERE f.tipoDocumento = :tipoDocumento"),
    @NamedQuery(name = "Factura.findByFecha", query = "SELECT f FROM Factura f WHERE f.fecha = :fecha"),
    @NamedQuery(name = "Factura.findByImporte", query = "SELECT f FROM Factura f WHERE f.importe = :importe"),
    @NamedQuery(name = "Factura.findByPagada", query = "SELECT f FROM Factura f WHERE f.pagada = :pagada")})
public class Factura implements Serializable {
    @Transient
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @Column(name = "iva")
    private int iva;
    @Basic(optional = false)
    @Column(name = "IRPF")
    private int irpf;
    @Basic(optional = false)
    @Column(name = "RazonSocial")
    private String razonSocial;
    @Basic(optional = false)
    @Column(name = "TipoDocumento")
    private String tipoDocumento;
    @Basic(optional = false)
    @Column(name = "Fecha")
    @Temporal(TemporalType.DATE)
    private Date fecha;
    @Basic(optional = false)
    @Column(name = "Importe")
    private float importe;
    @Column(name = "pagada")
    private Integer pagada;
    @OneToMany(mappedBy = "numeroFactura")
    private List<Cobros> cobrosList;
    @JoinColumn(name = "InmuebleAlquilado", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Inmueble inmuebleAlquilado;

    public Factura() {
    }

    public Factura(Integer id) {
        this.id = id;
    }

    public Factura(Integer id, int iva, int irpf, String razonSocial, String tipoDocumento, Date fecha, float importe) {
        this.id = id;
        this.iva = iva;
        this.irpf = irpf;
        this.razonSocial = razonSocial;
        this.tipoDocumento = tipoDocumento;
        this.fecha = fecha;
        this.importe = importe;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        Integer oldId = this.id;
        this.id = id;
        changeSupport.firePropertyChange("id", oldId, id);
    }

    public int getIva() {
        return iva;
    }

    public void setIva(int iva) {
        int oldIva = this.iva;
        this.iva = iva;
        changeSupport.firePropertyChange("iva", oldIva, iva);
    }

    public int getIrpf() {
        return irpf;
    }

    public void setIrpf(int irpf) {
        int oldIrpf = this.irpf;
        this.irpf = irpf;
        changeSupport.firePropertyChange("irpf", oldIrpf, irpf);
    }

    public String getRazonSocial() {
        return razonSocial;
    }

    public void setRazonSocial(String razonSocial) {
        String oldRazonSocial = this.razonSocial;
        this.razonSocial = razonSocial;
        changeSupport.firePropertyChange("razonSocial", oldRazonSocial, razonSocial);
    }

    public String getTipoDocumento() {
        return tipoDocumento;
    }

    public void setTipoDocumento(String tipoDocumento) {
        String oldTipoDocumento = this.tipoDocumento;
        this.tipoDocumento = tipoDocumento;
        changeSupport.firePropertyChange("tipoDocumento", oldTipoDocumento, tipoDocumento);
    }

    public Date getFecha() {
        return fecha;
    }

    public void setFecha(Date fecha) {
        Date oldFecha = this.fecha;
        this.fecha = fecha;
        changeSupport.firePropertyChange("fecha", oldFecha, fecha);
    }

    public float getImporte() {
        return importe;
    }

    public void setImporte(float importe) {
        float oldImporte = this.importe;
        this.importe = importe;
        changeSupport.firePropertyChange("importe", oldImporte, importe);
    }

    public Integer getPagada() {
        return pagada;
    }

    public void setPagada(Integer pagada) {
        Integer oldPagada = this.pagada;
        this.pagada = pagada;
        changeSupport.firePropertyChange("pagada", oldPagada, pagada);
    }

    @XmlTransient
    public List<Cobros> getCobrosList() {
        return cobrosList;
    }

    public void setCobrosList(List<Cobros> cobrosList) {
        this.cobrosList = cobrosList;
    }

    public Inmueble getInmuebleAlquilado() {
        return inmuebleAlquilado;
    }

    public void setInmuebleAlquilado(Inmueble inmuebleAlquilado) {
        Inmueble oldInmuebleAlquilado = this.inmuebleAlquilado;
        this.inmuebleAlquilado = inmuebleAlquilado;
        changeSupport.firePropertyChange("inmuebleAlquilado", oldInmuebleAlquilado, inmuebleAlquilado);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Factura)) {
            return false;
        }
        Factura other = (Factura) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "persistencia.Factura[ id=" + id + " ]";
    }

    public float restantePorPagar() {
        Iterator<Cobros> it = this.cobrosList.iterator();
        float parcial = 0;
        while(it.hasNext()){
            parcial = parcial + it.next().getImporte();
        }
        return this.importe - parcial;
    }

    public void pagarFactura() {
        this.pagada = 0;
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener(listener);
    }
    
}
